package com.zpark.resume.service.impl;

import com.zpark.resume.entity.Honor;
import com.zpark.resume.entity.Project;
import com.zpark.resume.exception.DeleteProjectLoseException;
import com.zpark.resume.exception.FindCvByIdIsNullException;
import com.zpark.resume.exception.InsertProjectLoseException;
import com.zpark.resume.exception.UpdateProjectLoseException;
import com.zpark.resume.mapper.IProjectMapper;
import com.zpark.resume.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PackageName: com.zpark.resume.service.impl
 * @ClassName: ProjectServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/6/1  22:02
 */
@Service
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private IProjectMapper projectMapper;

    /**
     * 根据cv的id查询数据
     * @param cvId
     * @return
     */
    @Override
    public List<Project> findByCvId(Integer cvId) {
        // 调用持久层接口查询数据
        List<Project> projects = projectMapper.selectByCvId(cvId);
        // 判断查询结果
        if (projects.size() <= 0) {
            // 结果为空，抛出异常FindCvByIdIsNullException("无效的ID")
            throw new FindCvByIdIsNullException("无效的ID");
        }
        return projects;
    }

    /**
     * 添加project
     * @param project
     * @return
     */
    @Override
    public Integer addProject(Project project) {
        // 判断project是否为null
        if (project == null) {
            // 抛出异常，添加项目经历失败
            throw new InsertProjectLoseException("添加项目经历失败异常");
        }
        // 调用持久层接口实现添加操作
        Integer row = projectMapper.insertProject(project);
        // 判断执行结果
        if (row <= 0) {
            // 抛出异常，添加项目经历失败
            throw new InsertProjectLoseException("添加项目经历失败异常");
        }
        // 返回受影响的行数
        return row;
    }

    /**
     * 修改项目经历
     * @param project
     * @return
     */
    @Override
    public Integer changeProjectById(Project project) {
        // 判断project是否为null
        if (project == null) {
            // 抛出异常，添加项目经历失败
            throw new UpdateProjectLoseException("修改项目经历失败异常");
        }
        // 调用持久层接口实现修改操作
        Integer row = projectMapper.updateProjectById(project);
        // 判断执行结果
        if (row <= 0) {
            // 抛出异常，添加项目经历失败
            throw new UpdateProjectLoseException("修改项目经历失败异常");
        }
        // 返回受影响的行数
        return row;
    }

    /**
     * 删除项目经历
     * @param id
     * @return
     */
    @Override
    public Integer removeProjectById(Integer id) {
        // 判断id是否为null或者是否为0
        if (id == null) {
            // 抛出异常，添加项目经历失败
            throw new DeleteProjectLoseException("删除项目经历失败异常");
        }
        // 调用持久层接口实现删除操作
        Integer row = projectMapper.deleteProjectById(id);
        // 判断执行结果
        if (row <= 0) {
            // 抛出异常，添加项目经历失败
            throw new DeleteProjectLoseException("删除项目经历失败异常");
        }
        // 返回受影响的行数
        return row;
    }
}
